//
//  HWGameLiveChatView.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/10/26.
//

import UIKit
import JXSegmentedView
class HWGameLiveChatView: UIView {
    var tableView:UITableView!
    let tableViewProtocol = HWGameLiveChatProtocol()
    var headImageClick:((_:HWVoiceRoomUserInfo)->())?
    
    var chatBottomView = HWGameLiveChatBottomView()
    /// 聊天
    var chatButtonClick:(()->())?
    /// 礼物按钮
    var giftButtonClick:(()->())?
    /// 管理
    var managerButtonClick:(()->())?
    var chatModelArr:[HWGameLiveChatModel]?{
        didSet {
            self.tableViewProtocol.chatModelArr = chatModelArr
            self.tableView.reloadData()
            if chatModelArr?.count ?? 0 > 0 {
                self.tableView.scrollToRow(at: IndexPath.init(row: chatModelArr!.count, section: 0), at: .bottom, animated: true)
            }
        }
    }
    
    var gameModel:HWVoiceRoomGameModel? {
        didSet {
            self.tableViewProtocol.gameModel = gameModel
            self.tableView.reloadData()
        }
    }
    var downLoadButtonClick:((_:HWVoiceRoomGameModel)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
        addAction()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension HWGameLiveChatView {
    private func initUI() -> Void {
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        addSubview(tableView)
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
        addSubview(chatBottomView)
        
        
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.left.right.top.equalTo(0)
            make.bottom.equalTo(chatBottomView.snp.top)
        }
        
        chatBottomView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            
            make.bottom.equalTo(0)
            if screenHeight < 812 {
                make.height.equalTo(60*widthScale)
            }else {
                make.height.equalTo(82*widthScale)
            }
        }
        
    }
}
extension HWGameLiveChatView {
    func addAction() ->  Void {
//        self.tableViewProtocol.headImageClick = {
//            self.headImageClick?($0)
//        }
        
        chatBottomView.chatButtonClick = {
            self.chatButtonClick?()

        }
        
        chatBottomView.giftButtonClick = {
            self.giftButtonClick?()
        }
        
        chatBottomView.managerButtonClick = {
            self.managerButtonClick?()
        }
        
        self.tableViewProtocol.downLoadButtonClick = {
            self.downLoadButtonClick?($0)
        }
    }
    
    
}
extension HWGameLiveChatView: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return self
    }
}
